package com.bangdream.michelia.view.fragment.main.curriculum;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.andview.refreshview.XRefreshView;
import com.bangdream.michelia.R;
import com.bangdream.michelia.contract.CoursesContract;
import com.bangdream.michelia.contract.TeacherContract;
import com.bangdream.michelia.entity.CommentBean;
import com.bangdream.michelia.entity.CoursesTypeBean;
import com.bangdream.michelia.entity.courses.CourseInfoBean;
import com.bangdream.michelia.entity.courses.CoursesBean;
import com.bangdream.michelia.entity.TeacherBean;
import com.bangdream.michelia.entity.courses.TeacherListBean;
import com.bangdream.michelia.presenter.CoursesPresenter;
import com.bangdream.michelia.presenter.TeacherPresenter;
import com.bangdream.michelia.tool.CircleImageView;
import com.bangdream.michelia.utils.ImageLoadUtils;
import com.bangdream.michelia.utils.L;
import com.bangdream.michelia.utils.PublicResources;
import com.bangdream.michelia.utils.TextViewUtil;
import com.bangdream.michelia.view.activity.lecturer.LecturerDetails;
import com.bangdream.michelia.view.fragment.currency.BaseFragment;
import com.bangdream.michelia.view.fragment.currency.LazyLoadFragment;
import com.nanchen.compresshelper.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @date on 2018/7/23 17:36
 * @email 909732184@qq.com
 * 课程详情-讲师
 */
public class CurriculumDetailsLecturer extends LazyLoadFragment implements CoursesContract.ICoursesView {
    private View view;
    private XRefreshView refreshView;
    private CourseInfoBean data;
    private TeacherBean teacherBean;
    private TextView tvIdentity;
    private TextView tvContent;
    private TextView btnMore;
    private TextView tvCurriculumNumber;
    private TextView tvName;
    private CircleImageView ivLogo;
    private RelativeLayout emptyView;
    private LinearLayout dataView;
    private CoursesPresenter coursesPresenter;


    @Override
    public void onDestroy() {
        super.onDestroy();
        if(coursesPresenter!=null){
            coursesPresenter.detach();
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.curriculum_lecturer,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        setListener();
    }

    private void setListener() {
        btnMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(teacherBean!=null) {
                    Intent intent = new Intent(getActivity(),LecturerDetails.class);
                    intent.putExtra("id", teacherBean.getId());
                    startActivity(intent);
                }else {
                    showText("没有讲师信息");
                }
            }
        });
    }

    @Override
    public TeacherPresenter createPresenter() {
        return new TeacherPresenter();
    }

    @Override
    public void loadData() {
        setViewData(data);
   }

    private void initView() {
        if(getArguments()!=null){
            data = getArguments().getParcelable("data");
        }
        coursesPresenter = new CoursesPresenter();
        coursesPresenter.attach(this);
        refreshView = view.findViewById(R.id.refreshView);
        btnMore = view.findViewById(R.id.btnMore);
        refreshView.setPullRefreshEnable(false);
        refreshView.setPullLoadEnable(false);
        tvIdentity = view.findViewById(R.id.tvIdentity);
        tvContent = view.findViewById(R.id.tvContent);
        tvCurriculumNumber = view.findViewById(R.id.tvCurriculumNumber);
        tvName = view.findViewById(R.id.tvName);
        ivLogo = view.findViewById(R.id.ivLogo);
        emptyView = view.findViewById(R.id.emptyView);
        dataView = view.findViewById(R.id.dataView);
    }

    private void setViewData(CourseInfoBean data) {
        this.data = data;
        if(data==null) {
            return;
        }
        L.d(data.toString());
//        if(teacherListBean!=null) {
//            mPresenter.getTeacherDetails(teacherListBean.getTeacherId());
//            Map paramsMap = new HashMap();
//            List<String> teacherIdsList = new ArrayList<>();
//            teacherIdsList.add(teacherListBean.getId());
//            String teacherIds = JSON.toJSONString(teacherIdsList);
//            L.e("teacherIds"+teacherIds);
//            //paramsMap.put("teacherIds", teacherIds);
//            paramsMap.put("courseOrTeacherName", teacherListBean.getTeacherName());
//            coursesPresenter.getTeacherCoursesListData(1, PublicResources.PageSize,
//                    paramsMap, null);
//        }

    }


    public void setView(TeacherBean data) {
        if(data!=null){
            emptyView.setVisibility(View.GONE);
            refreshView.setVisibility(View.VISIBLE);
            teacherBean = data;
            ImageLoadUtils.ImageLoad(getActivity(),teacherBean.getFaceUrl(),ivLogo,-1);

            tvName.setText(StringUtil.isEmpty(data.getName())?"暂无":data.getName()+"");
            if(data.getTitle()!=null)
            tvIdentity.setText(Html.fromHtml(data.getTitle()+""));
            if(data.getContent()!=null)
                tvContent.setText(Html.fromHtml(data.getContent()+""));

            tvCurriculumNumber.setText(data.getCourseCount()+"");
        }else{

        }
    }

    @Override
    public void setCoursestList(boolean isSuccess, int page, CoursesContract.CoursesType type, List<CoursesBean> data, String stDescribe) {
        if(isSuccess){
            if(data!=null)
            tvCurriculumNumber.setText(data.size()+"");
        }
    }

    @Override
    public void setCoursesDetails(boolean isSuccess, CoursesBean data, String stDescribe) {

    }

    @Override
    public void setCoursesTypeList(boolean isSuccess, List<CoursesTypeBean> data, String stDescribe) {

    }

    @Override
    public void setCommentList(boolean isSuccess, int page, List<CommentBean> data, String stDescribe) {

    }

    @Override
    public void actionComment(boolean isSuccess, String stDescribe) {

    }
}
